package com.example.aijiancece;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.switchmaterial.SwitchMaterial;

public class PrivacySettingsActivity extends AppCompatActivity {

    private static final String PREFS_NAME = "privacy_settings";
    private static final String KEY_SAVE_HISTORY = "save_history";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_settings);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        SwitchMaterial switchHistory = findViewById(R.id.switchHistory);
        TextView tvClearHistory = findViewById(R.id.tvClearHistory);
        TextView tvPrivacyPolicy = findViewById(R.id.tvPrivacyPolicy);

        toolbar.setNavigationOnClickListener(v -> finish());

        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        switchHistory.setChecked(prefs.getBoolean(KEY_SAVE_HISTORY, true));

        switchHistory.setOnCheckedChangeListener((buttonView, isChecked) -> {
            prefs.edit().putBoolean(KEY_SAVE_HISTORY, isChecked).apply();
            // TODO: 更新历史记录设置
        });

        tvClearHistory.setOnClickListener(v -> showClearHistoryDialog());

        tvPrivacyPolicy.setOnClickListener(v -> {
            // TODO: 替换为实际的隐私政策URL
            String url = "https://example.com/privacy-policy";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        });
    }

    private void showClearHistoryDialog() {
        new MaterialAlertDialogBuilder(this)
                .setTitle("清除历史记录")
                .setMessage("确定要清除所有检测历史记录吗？此操作无法撤销。")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    // TODO: 清除历史记录
                    Toast.makeText(this, "历史记录已清除", Toast.LENGTH_SHORT).show();
                })
                .show();
    }
} 